package com.yz.xyt.controller;

import com.yz.xyt.constant.Constants;
import com.yz.xyt.constant.HttpStatus;
import com.yz.xyt.domain.ResponseResult;
import com.yz.xyt.domain.Student;
import com.yz.xyt.domain.User;
import com.yz.xyt.domain.vo.StaffVo;
import com.yz.xyt.domain.vo.StudentVo;
import com.yz.xyt.service.IStaffService;
import com.yz.xyt.service.IStudentService;
import com.yz.xyt.service.IUserService;
import com.yz.xyt.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 系统用户Controller
 * @author JW.WANG
 */
@RestController
@RequestMapping("/system")
public class UserController {

    @Autowired
    IUserService userService;

    @Autowired
    IStudentService studentService;

    @Autowired
    IStaffService staffService;

    /**
     * 重置账号密码
     * @param user
     * @return
     */
    @RequestMapping("/resetPwd")
    public ResponseResult resetPwd(User user){
        ResponseResult result = new ResponseResult();
        //重置密码
        int count = userService.resetPassword(user);
        if(count > 0){
            result.setCode(HttpStatus.SUCCESS);
            result.setMsg("重置密码成功");
        }else{
            result.setCode(HttpStatus.NOT_IMPLEMENTED);
            result.setMsg("重置密码失败");
        }

        result.setData(null);
        return result;
    }

    /**
     * 查询个人信息
     * @return
     */
    @RequestMapping("/queryUserInfo")
    public ResponseResult queryUserInfo(){
        ResponseResult result = new ResponseResult();
        result.setMsg("查询成功");
        result.setCode(HttpStatus.SUCCESS);
        List<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(roles.contains(Constants.ROLE_STUDENT)){
           StudentVo studentInfo = studentService.findStudentInfo(SecurityUtils.getUsername());
            if (studentInfo == null){
                result.setCode(HttpStatus.ERROR);
                result.setMsg("查询失败,个人信息缺失");
            }else {
                result.setData(studentInfo);
            }
        }else{
            StaffVo staffInfo = staffService.findStaffInfo(SecurityUtils.getUsername());
            if (staffInfo == null){
                result.setCode(HttpStatus.ERROR);
                result.setMsg("查询失败,个人信息缺失");
            }else {
                result.setData(staffInfo);
            }
        }

        return  result;
    }

    /**
     *
     * @param user
     * @return
     */
    @RequestMapping("/updatePwd")
    public ResponseResult updatePassword(User user){
        user.setId(SecurityUtils.getUserId());
        String password = user.getPassword();
        user.setPassword(SecurityUtils.encryptPassword(password));
        userService.updateByPrimaryKeySelective(user);
        ResponseResult result = new ResponseResult();
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("修改密码成功");
        return result;
    }
}
